package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.constant.TaskConstant;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.CouponsRequestDTO;
import com.example.demo.mapper.BadgeManageMapper;
import com.example.demo.mapper.ClothingManageMapper;
import com.example.demo.mapper.CouponsManageMapper;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.CouponsManageService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CouponsManageServiceImpl extends ServiceImpl<CouponsManageMapper, Coupons> implements CouponsManageService {
    @Autowired
    private CouponsManageMapper mapper;
    @Override
    public PageVo<CouponsView> getVoucherList(PageBo pageBo) {
        List<CouponsView> logs = mapper.getVoucherList();
        for (CouponsView nowCoupons : logs) {
            if(nowCoupons.getState()==1){
                nowCoupons.setOpenState(true);
                nowCoupons.setEditState(false);
            } else if (nowCoupons.getState()==2) {
                nowCoupons.setOpenState(false);
                nowCoupons.setEditState(false);
            }else if (nowCoupons.getState()==3) {
                nowCoupons.setOpenState(false);
                nowCoupons.setEditState(true);
            }
        }
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<CouponsView> getFullDiscountCouponList(PageBo pageBo) {
        List<CouponsView> logs = mapper.getFullDiscountCouponList();
        for (CouponsView nowCoupons : logs) {
            if(nowCoupons.getState()==1){
                nowCoupons.setOpenState(true);
                nowCoupons.setEditState(false);
            } else if (nowCoupons.getState()==2) {
                nowCoupons.setOpenState(false);
                nowCoupons.setEditState(false);
            }else if (nowCoupons.getState()==3) {
                nowCoupons.setOpenState(false);
                nowCoupons.setEditState(true);
            }
        }
        return new PageVo<>(pageBo, logs);
    }
    @Override
    public PageVo<CouponsView> getDiscountCouponList(PageBo pageBo) {
        List<CouponsView> logs = mapper.getDiscountCouponList();
        for (CouponsView nowCoupons : logs) {
            if(nowCoupons.getState()==1){
                nowCoupons.setOpenState(true);
                nowCoupons.setEditState(false);
            } else if (nowCoupons.getState()==2) {
                nowCoupons.setOpenState(false);
                nowCoupons.setEditState(false);
            }else if (nowCoupons.getState()==3) {
                nowCoupons.setOpenState(false);
                nowCoupons.setEditState(true);
            }
        }
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<CouponsView> getExchangeCouponList(PageBo pageBo) {
        List<CouponsView> logs = mapper.getExchangeCouponList();
        for (CouponsView nowCoupons : logs) {
            if(nowCoupons.getState()==1){
                nowCoupons.setOpenState(true);
                nowCoupons.setEditState(false);
            } else if (nowCoupons.getState()==2) {
                nowCoupons.setOpenState(false);
                nowCoupons.setEditState(false);
            }else if (nowCoupons.getState()==3) {
                nowCoupons.setOpenState(false);
                nowCoupons.setEditState(true);
            }
        }
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<CouponsView> searchCouponList(CouponsView request, PageBo pageBo) {
        String couponName = request.getCouponsName();
        String advertiserName = request.getCompanyName();
        int couponType = request.getCouponsType();
        List<CouponsView> list = mapper.getCouponList(couponType);
        list = list.stream()
                .filter(o ->(couponName == null || o.getCouponsName().contains(couponName))&&
                            (advertiserName == null || o.getCompanyName().contains(advertiserName)))
                .collect(Collectors.toList());
        return new PageVo<>(pageBo, list);
    }

    @Override
    public Result<String> addCoupons(CouponsRequestDTO coupons, String fileName) {
        Coupons request = setCoupons(coupons,fileName);

        System.out.println("addCoupons返回值"+request);
        if(request!=null){
            if (mapper.addCoupons(request) > 0) {
                return Result.ok("添加成功");
            } else {
                return Result.fail("添加失败");
            }
        }
        return Result.fail("添加失败,填写内容为空");
    }

    @Override
    public List<CouponsView> getCompanyList() {
        List<CouponsView> logs = mapper.getCompanyList();
        return logs;
    }

    @Override
    public List<CouponsView> getCouponDataList(int couponsId) {
        List<CouponsView> logs = mapper.getCouponDataList(couponsId);
        return logs;
    }

    @Override
    public Result<String> Coupons(Coupons coupons) {
        if(coupons!=null){
            if(mapper.deleteById(coupons)>0)
                return Result.ok("删除成功");
            else
                return Result.fail("删除失败");
        }
        return Result.fail("删除失败,选择内容为空");
    }

    @Override
    public void batchDeleteCoupons(List<Coupons> couponsList) {
        mapper.batchDeleteClothing(couponsList);
    }

    @Override
    public Result<String> updateState(CouponsView request) {
        if(request.getOpenState()==true){
            request.setState(1);
        } else if (request.getOpenState()==false) {
            request.setState(2);
        }

        if(request!=null){
            mapper.updateState(request);
            return Result.ok("修改成功");
        }
        else {
            return Result.fail("修改失败");
        }
    }

    @Override
    public String getPicById(int couponsId) {
        return mapper.selectPicById(couponsId);
    }

    @Override
    public void placeCouponsPic(int couponsId, String fileName) {
        Coupons request = new Coupons();
        if(couponsId!=0){
            request.setCouponsId(couponsId);
            request.setProductImageUrl(fileName);
            mapper.placeCouponsPic(request);
        }
    }

    @Override
    public PageVo<StudentCoupons> getStudentCouponsList(PageBo pageBo) {
        List<StudentCoupons> logs = mapper.getStudentCouponsList();
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<StudentCoupons> getRedeemList(PageBo pageBo) {
        List<StudentCoupons> logs = mapper.getRedeemList();
        return new PageVo<>(pageBo, logs);
    }


    private Coupons setCoupons(CouponsRequestDTO coupons,String fileName) {
        Coupons request = new Coupons();
        request.setCouponsName(coupons.getCouponsName());
        request.setCouponsType(coupons.getType());
        request.setMinimumAmount(coupons.getMinimumAmount());
        request.setProductName(coupons.getProductName());
        request.setCondition(coupons.getCondition());
        request.setDescription(coupons.getDescription());
        request.setAdvertiserId(1);
        request.setValue(coupons.getValue());
        request.setProductImageUrl(fileName);
        request.setState(1);

        List<String> dateRange= coupons.getDateRange();
        if (dateRange != null && dateRange.size() == 2) {

            DateTimeFormatter inputFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; // 用于解析输入的ISO 8601格式

            LocalDateTime startDate = LocalDateTime.parse(dateRange.get(0), inputFormatter);
            LocalDateTime endDate = LocalDateTime.parse(dateRange.get(1), inputFormatter);

            DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            String formattedStartDate = startDate.format(outputFormatter);
            String formattedEndDate = endDate.format(outputFormatter);

            request.setStartDate(formattedStartDate);
            request.setEndDate(formattedEndDate);

            // 或者直接返回这两个字符串
            System.out.println("Start Date: " + formattedStartDate);
            System.out.println("End Date: " + formattedEndDate);
        } else {
            throw new IllegalArgumentException("Invalid date range. It should contain exactly two elements.");
        }

        return request;

    }


}
